package cn.edu.scau.cmi.yejiancong.ORM.client;


import java.util.List;

import cn.edu.scau.cmi.yejiancong.ORM_hibernate.DAO.studentDAO;
import cn.edu.scau.cmi.yejiancong.ORM_hibernate.DAO.teacherDAO;
import cn.edu.scau.cmi.yejiancong.ORM_hibernate.entiry.Student;
import cn.edu.scau.cmi.yejiancong.ORM_hibernate.entiry.Teacher;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

public class seeDetailController {
	@FXML
	private TreeView<String> treeview =new TreeView<String>();
	@FXML
	private Label studentid;
	@FXML
	private Label studentname;
	@FXML
	private TextField id;
	@FXML
	private Button del;
	@FXML
	private Button add;
	@FXML
	private Button update;
	private int studentorteacher=client.select;
	private Teacher T=null;
	private Student S=null;
	private TreeItem<String> item;
	public static String stid=seeviewController.studentids;
	//初始化，加树表
	
	 public seeDetailController() {
		// TODO Auto-generated constructor stub
	}
	
	@FXML
	public void initialize() {
		

		studentid.setText(stid);
		if(studentorteacher==0) {
			
			S=studentDAO.get(Integer.parseInt(stid));
			
			studentname.setText(S.getStudentname());
		}
		else if(studentorteacher==1) {
			T=teacherDAO.get(Integer.parseInt(stid));
			studentname.setText(T.getTeachername());
		}
	
	
		
		item=new TreeItem<>("list");
		if(studentorteacher==0) {
			TreeItem<String> treeItem=new TreeItem<String>("teacher");
			List<Object[]> list=studentDAO.getTeach(S.getStudentid());
			for(Object[] o:list) {
				Teacher teacher=(Teacher)o[1];
				TreeItem<String> ti=new TreeItem<>(teacher.getTeacherid()+":"+teacher.getTeachername());
				treeItem.getChildren().add(ti);
			}
			TreeItem<String> treeItem2=new TreeItem<String>("tutor:"+S.getTutor().getTeacherid()+":"+S.getTutor().getTeachername());
			item.getChildren().add(treeItem);
			item.getChildren().add(treeItem2);
				treeview.setRoot(item);		
		}
		if(studentorteacher==1) {
			TreeItem<String> treeItem=new TreeItem<String>("teach student");
			TreeItem<String> treeItem2=new TreeItem<String>("tutor student");
			List<Object[]> list=teacherDAO.getstudent(T.getTeacherid());
			
			for(Object[] o:list) {
				Student Student=(Student)o[1];
				TreeItem<String> ti=new TreeItem<>(Student.getStudentid()+":"+Student.getStudentname());
				treeItem.getChildren().add(ti);
				Student tStudent=(Student)o[2];
				TreeItem<String> ts=new TreeItem<>(Student.getStudentid()+":"+Student.getStudentname());
				treeItem2.getChildren().add(ts);
			}
			
			
			item.getChildren().add(treeItem);
			item.getChildren().add(treeItem2);
				treeview.setRoot(item);		
		}
		listen();
	}
	
	
	//加对象
	public void add() {
		if(studentorteacher==0) {
			Teacher aTeacher=null;
			if(selecttiem.getValue().matches("tutor*")) {
				System.out.println("fales");
			}
			else {
				aTeacher=teacherDAO.get(Integer.parseInt(getid()));
				if(aTeacher!=null ) {
					Student student=studentDAO.get(Integer.parseInt(studentid.getText()));
					student.getTeachers().add(aTeacher);
					TreeItem<String> s=new TreeItem<>(aTeacher.getTeacherid()+":"+aTeacher.getTeachername());
					item.getChildren().add(s);
					treeview.setRoot(item);
				}
				else {
					System.out.println("fales");
				}
			}
		}
		if(studentorteacher==1) {
			Teacher teacher=teacherDAO.get(Integer.parseInt(getid()));
			Student student=studentDAO.get(Integer.parseInt(getid()));
			if(student!=null) {
			if(selecttiem.getParent().getValue().equals("teach student")) {
				teacher.getStudents().add(student);
				TreeItem<String> treeItem=new TreeItem<String>(student.getStudentid()+":"+student.getStudentname());
				item.getChildren().get(0).getChildren().add(treeItem);
			}
			else if(selecttiem.getParent().getValue().equals("tutor student")) {
				teacher.getIntroducerStudent().add(student);
				TreeItem<String> treeItem=new TreeItem<String>(student.getStudentid()+":"+student.getStudentname());
				item.getChildren().get(1).getChildren().add(treeItem);
			}
		}
		}
	}
	//改对象
	public void update() {
		
	}
	
	//删对象
	public void del() {
		if(studentorteacher==0) {
			Student student=studentDAO.get(Integer.parseInt(studentid.getText()));
			Teacher teacher=teacherDAO.get(Integer.parseInt(getid()));
			student.getTeachers().remove(teacher);
			item.getChildren().remove(selecttiem);
		}
		if(studentorteacher==1) {
			Teacher teacher=teacherDAO.get(Integer.parseInt(getid()));
			Student student=studentDAO.get(Integer.parseInt(getid()));
			if(selecttiem.getParent().getValue().equals("teach student")) {
				teacher.getStudents().remove(student);
			}
			else if(selecttiem.getParent().getValue().equals("tutor student")) {
				teacher.getIntroducerStudent().remove(student);
			}
			studentDAO.del(Integer.parseInt(studentid.getText()));
			item.getChildren().remove(selecttiem);
		}
	}
	public String getid() {
		return selecttiem.getValue().replaceAll(":|[a-z]*[A-Z]*", "");
		
	}
	public String getname() {
		return selecttiem.getValue().replaceAll(":|[0-9]*", "");
	}

	private TreeItem<String> selecttiem;
	//监听树表点击
			@SuppressWarnings("unchecked")
			public void listen() {
				treeview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
					@Override
					public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue,
							TreeItem<String> newValue) {
						 selecttiem=newValue;
						
					}
				});
			}

}
